package presenter;

import android.content.Context;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

import ui.view.IBaseView;

/**
 * Created by 10153 on 2018/7/14.
 */

public class BasePresenter<GV extends IBaseView> {
    
    protected Reference<GV> mViewRef;
    
    public BasePresenter(GV view) {
        onAttachView(view);
        onCreate();
    }
    
    public void onAttachView(GV view){
        mViewRef = new WeakReference<>(view);
    }
    
    public void onDetachView(){
        if(mViewRef != null){
            mViewRef.clear();
            mViewRef = null;
        }
    }
    
    protected void onCreate(){
    
    }
    
    public void onStart(){
        if(!isViewAttached()){
            System.out.print("Presenter:"+this.getClass().getName()+" hasn't attach view!");
            return;
        }
    }
    
    public void onPause(){
    
    }
    
    public void onResume(){

    }
    
    public void onStop(){
    
    }
    
    public void onDestroy(){
    
    }
    
    public boolean isViewAttached(){
        return mViewRef != null && mViewRef.get() != null;
    }
    
    protected GV getView(){
        return mViewRef.get();
    }
    
    protected Context getContext(){
        return (Context)mViewRef.get();
    }
}
